package com.atguigu.edu.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.edu.bean.TradeProvinceOrderAmount;
import com.atguigu.edu.realtime.common.bean.Traffic_Statistics_From_Each_Hour;
import com.atguigu.edu.realtime.common.bean.Traffic_Statistics_From_Each_Source;
import com.atguigu.edu.service.trafficStatsService;
import com.atguigu.edu.util.DateFormatUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class traffic_StatisticsController {
    // TODO: 2024/11/12 装配 service类
    @Autowired
    private trafficStatsService trafficVisitService;
    @RequestMapping("/sc")
    public JSONObject getScUvCt(@RequestParam(value = "date",defaultValue = "0") Integer date){

        List<Traffic_Statistics_From_Each_Source> traffic_statistics = trafficVisitService.getTraffic_Statistics(date);
        // TODO: 2024/11/18 构建返回的 json
        JSONObject js0=new JSONObject();
        js0.put("status", 0);
        js0.put("msg", "");

        JSONObject datas = new JSONObject();

        // TODO: 2024/11/17 存放 rows 的 list
        List<JSONObject> rows = new ArrayList<>();
        // TODO: 2024/11/17 存放 columns(JSONObject) 的 list
        List<JSONObject> scList1 = new ArrayList<>();

        for (Traffic_Statistics_From_Each_Source trafficUvCt : traffic_statistics) {
            JSONObject js1=new JSONObject();
            js1.put("sc",trafficUvCt.getSc());
            js1.put("uv_ct",trafficUvCt.getUv_ct());
            js1.put("sv_ct",trafficUvCt.getSv_ct());
            js1.put("avg_pv_sv",trafficUvCt.getAvg_pv_sv());
            js1.put("avg_dur_sv",trafficUvCt.getAvg_dur_sv());
            rows.add(js1);
        }

        JSONObject js2=new JSONObject();
        js2.put("name","来源");
        js2.put("id","sc");
        scList1.add(js2);
        JSONObject js3=new JSONObject();
        js3.put("name","独立访客数");
        js3.put("id","uv_ct");
        scList1.add(js3);
        JSONObject js4=new JSONObject();
        js4.put("name","会话总数");
        js4.put("id","sv_ct");
        scList1.add(js4);
        JSONObject js5=new JSONObject();
        js5.put("name","会话平均浏览页面数");
        js5.put("id","avg_pv_sv");
        scList1.add(js5);
        JSONObject js6=new JSONObject();
        js6.put("name","会话平均停留时长");
        js6.put("id","avg_dur_sv");
        scList1.add(js6);


        // TODO: 2024/11/18 添加  columns
        datas.put("columns",scList1);
        // TODO: 2024/11/18 添加 rows 属性
        datas.put("rows",rows);

        // TODO: 2024/11/18 添加 data 属性
        js0.put("data",datas);

        return js0;
    }


    @RequestMapping("/schour")
    public JSONObject getScUvCthour(@RequestParam(value = "date",defaultValue = "0") Integer date){
        List<Traffic_Statistics_From_Each_Hour> traffic_statistics = trafficVisitService.getTraffic_Statistics_hour(20241116);
        // TODO: 2024/11/18 构建返回的 json
        com.alibaba.fastjson.JSONObject js0=new com.alibaba.fastjson.JSONObject();
        js0.put("status", 0);
        js0.put("msg", "");

        com.alibaba.fastjson.JSONObject datas = new com.alibaba.fastjson.JSONObject();

        // TODO: 2024/11/17 存放 rows 的 list
        List<com.alibaba.fastjson.JSONObject> rows = new ArrayList<>();
        // TODO: 2024/11/17 存放 columns(JSONObject) 的 list
        List<com.alibaba.fastjson.JSONObject> scList1 = new ArrayList<>();

        for (Traffic_Statistics_From_Each_Hour trafficUvCt : traffic_statistics) {
            com.alibaba.fastjson.JSONObject js1=new com.alibaba.fastjson.JSONObject();
            js1.put("hour",trafficUvCt.getHour());
            js1.put("uv_ct",trafficUvCt.getUv_ct());
            js1.put("sv_ct",trafficUvCt.getPv_ct());
            js1.put("avg_pv_sv",trafficUvCt.getAvg_pv_sv());
            rows.add(js1);
        }

        com.alibaba.fastjson.JSONObject js4=new com.alibaba.fastjson.JSONObject();
        js4.put("name","小时");
        js4.put("id","hour");
        scList1.add(js4);

        com.alibaba.fastjson.JSONObject js2=new com.alibaba.fastjson.JSONObject();
        js2.put("name","独立访客数");
        js2.put("id","uv_ct");
        scList1.add(js2);

        com.alibaba.fastjson.JSONObject js3=new com.alibaba.fastjson.JSONObject();
        js3.put("name","页面浏览数");
        js3.put("id","sv_ct");
        scList1.add(js3);

        com.alibaba.fastjson.JSONObject js5=new com.alibaba.fastjson.JSONObject();
        js5.put("name","会话平均浏览页面数");
        js5.put("id","avg_pv_sv");
        scList1.add(js5);

        // TODO: 2024/11/18 添加  columns
        datas.put("columns",scList1);
        // TODO: 2024/11/18 添加 rows 属性
        datas.put("rows",rows);

        // TODO: 2024/11/18 添加 data 属性
        js0.put("data",datas);

        return js0;

    }

    }
